package com.weiyuwqj.controller.debt;

import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.BasDebt;
import com.weiyuwqj.api.model.BasItem;
import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/7/6.
 */
@Controller
public class DebtController {

    @Autowired
    BasDebtService debtService;
    @Autowired
    BusAccountService accountService;
    @Autowired
    BasItemService itemService;
    @Autowired
    BusInvestService investService;
    @Autowired
    BusInvestCollectService investCollectService;

    /**
     * 债权的购买
     * @return 购买结果
     */
    @ResponseBody
    @RequestMapping(value = "/user/debt/buy")
    public Object debtBuy(Double amount, Integer debtId,
                          @RequestParam(defaultValue = "2") Integer investType, HttpServletRequest request,String payPassword) {

        Integer userId=(Integer)request.getSession().getAttribute("userId");

        Map map = null; //debtService.doInvest(userId, new BigDecimal(amount), debtId, 9,payPassword);
        //满标状态复审
        String debtScale = map.get("debtScale").toString();
        if ("100".equals(debtScale)) {
           // debtService.debtCheckSuccess(debtId);
        }
        return Message.OK;
    }

    /**
     * 发布债权
     * @param debt
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/user/debt/addDebt")
    public Object addDebt (BasDebt debt,HttpServletRequest request,String payPassword){
        Integer userId=(Integer)request.getSession().getAttribute("userId");
        debt.setUserId(userId);
        //debtService.addDebt(debt,payPassword);
        return Message.OK;
    }

    /**
     * 债转转让列表
     * @param p
     * @param days
     * @param rate
     * @param amount
     * @param itemRepayMethod
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/debt/investList")
    public Object debtInvestList(Paginator p,Integer days,Integer rate,Integer amount,Integer itemRepayMethod){
        Map map =new HashMap();
        if(days == 1){
            map.put("daysMin",30);
            map.put("daysMax",60);
        }else if(days == 2){
            map.put("daysMin",60);
            map.put("daysMax",90);
        } else if(days == 3){
            map.put("daysMin",90);
            map.put("daysMax",180);
        }else if(days == 4){
            map.put("daysMin",180);
        }
        if(rate == 1){
            map.put("rateMin",7);
            map.put("rateMax",8);
        }else if(rate == 2){
            map.put("rateMin",8);
            map.put("rateMax",10.88);
        } else if(rate == 3){
            map.put("rateMin",10.88);
            map.put("rateMax",12.68);
        }
        if(amount == 1){
            map.put("amountMin",100);
            map.put("amountMax",10000);
        }else if(amount == 2){
            map.put("amountMin",10000);
            map.put("amountMax",100000);
        } else if(amount == 3){
            map.put("amountMin",100000);
            map.put("amountMax",1000000);
        }
        map.put("itemRepayMethod",itemRepayMethod);
        //return debtService.getDebtInvestList(p,map);
        return null;
    }

    /**
     * 债权详情
     * @return
     */
    @RequestMapping(value = "/debt/getDebtDetail/{debtId}")
    public Object getDebtDetail(@PathVariable Integer debtId, HttpServletRequest request) {
        //投资用户资产信息
        Integer userId=(Integer)request.getSession().getAttribute("userId");
        if(userId != null){
            request.getSession().setAttribute("user", accountService.getBusAccountByUserId(userId));
        }
        //查询债权信息
        BasDebt debt = debtService.selectByKey(debtId);
        //查询标的
        BasItem item = itemService.selectByKey(debt.getItemId());
        //计算剩余时间
        Long time = new Long(0);//debt.getEndTime().getTime() - new Date().getTime();
        Long HH = time / 3600000 ;
        Long mm =(time - HH * 3600000) / 60000;
        Long ss = (time - HH * 3600000 - mm * 60000)/1000;
        String strTime = HH+"时"+mm+"分"+ss+"秒";
//        SimpleDateFormat sdf = new SimpleDateFormat("HH时mm分ss秒");
//        String strTime = sdf.format(time);
        request.getSession().setAttribute("debt", debt);
        request.getSession().setAttribute("item", item);
        request.getSession().setAttribute("strTime", strTime);
        return "main/investment/debtDtails";
    }

    /**
     * 根据债权id查询债权投资记录
     * @param p
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/debt/getDebtInvestList")
    public Object getDebtInvestList(Paginator p,Integer id){
        return null;
        //return investService.getDebtInvestList(p, id);
    }

    /**
     * 根据债权id查询发布债权人的待收记录
     * @param p
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/debt/getDebtCollectList")
    public Object getDebtCollectList(Paginator p,Integer id){
        BasDebt debt = debtService.selectByKey(id);
        return null;
        //return investCollectService.getNotRepayCollectListByInvestId(p,debt.getInvestId());
    }

}
